package org.jeecg.modules.rooms.controller;


import cn.hutool.core.util.EnumUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.Enum.ResultCode;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.rooms.DTO.CesAllDayPriceRuleDto;
import org.jeecg.modules.rooms.Enum.CouponEnums;
import org.jeecg.modules.rooms.entity.CesAllDayPriceRule;
import org.jeecg.modules.rooms.entity.CesAllDayRuleMinute;
import org.jeecg.modules.rooms.service.CesAllDayPriceRuleServiceImpl;
import org.jeecg.modules.rooms.service.CesAllDayRuleMinuteServiceImpl;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 全天房计费规则表 前端控制器
 * </p>
 *
 * @author Claude
 * @since 2023-03-07
 */
@RestController
@RequestMapping("/rooms/cesAllDayPriceRule")
@Api(tags = "ces_all_day_price_rule")
@Slf4j
public class CesAllDayPriceRuleController  extends JeecgController<CesAllDayPriceRule, CesAllDayPriceRuleServiceImpl> {
    @Resource
    CesAllDayPriceRuleServiceImpl cesAllDayPriceRuleService;

    @ApiOperation(value="全天房计费统一设置超时收费", notes="全天房计费统一设置超时收费")
    @PostMapping(value = "/modify")
    public Result modify(@RequestBody CesAllDayPriceRuleDto cesAllDayPriceRuleDto){
        if (null==cesAllDayPriceRuleDto){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if (StringUtils.isBlank(cesAllDayPriceRuleDto.getHotelId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if (StringUtils.isBlank(cesAllDayPriceRuleDto.getEnterTime())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if (StringUtils.isBlank(cesAllDayPriceRuleDto.getLeaveTime())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if (cesAllDayPriceRuleDto.getTimeOutRule().intValue()!= 0){
            List<Object> codes = EnumUtil.getFieldValues(CouponEnums.ALLDAYROOMRULETIMEOUT.class, "code");
            if(!codes.contains(cesAllDayPriceRuleDto.getTimeOutRule().intValue())){
                return Result.error("参数不正确!请选择正确的收费类型!");
            }
        }else{
            return Result.error(ResultCode.PARAM_MISS);
        }
        if (StringUtils.isBlank(cesAllDayPriceRuleDto.getEndTime())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if (cesAllDayPriceRuleDto.getRuleType().intValue()!=0){
            List<Object> codes = EnumUtil.getFieldValues(CouponEnums.ALLDAYROOMRULETYPEDETAILS.class, "code");
            if (!codes.contains(cesAllDayPriceRuleDto.getTimeOutRule().intValue())){
                return Result.error("参数不正确！请选择正确的收费规则！");
            }
        }else {
            return Result.error(ResultCode.PARAM_MISS);
        }
        //按分钟收费验证逻辑
        if(CouponEnums.ALLDAYROOMRULETIMEOUT.MINUTE.code() == cesAllDayPriceRuleDto.getTimeOutRule().intValue()){
            //统一设置验证参数
            if(CouponEnums.ALLDAYROOMRULETYPEDETAILS.UNIFY.code() == cesAllDayPriceRuleDto.getRuleType().intValue()){
                //验证字段参数是否正确
                if (null==cesAllDayPriceRuleDto.getMinute()||1>cesAllDayPriceRuleDto.getMinute().intValue()){
                    return Result.error(ResultCode.PARAM_MISS);
                }
                if (null==cesAllDayPriceRuleDto.getPrice()||BigDecimal.ZERO.compareTo(cesAllDayPriceRuleDto.getPrice())>0){
                    return Result.error(ResultCode.PARAM_MISS);
                }
                if (null==cesAllDayPriceRuleDto.getMoreThenMinute()||1>cesAllDayPriceRuleDto.getMoreThenMinute().intValue()){
                    return Result.error(ResultCode.PARAM_MISS);
                }
                if (null==cesAllDayPriceRuleDto.getMoreThenPrice()||BigDecimal.ZERO.compareTo(cesAllDayPriceRuleDto.getMoreThenPrice())>0){
                    return Result.error(ResultCode.PARAM_MISS);
                }
            }else if(CouponEnums.ALLDAYROOMRULETYPEDETAILS.LAYOUT.code() == cesAllDayPriceRuleDto.getRuleType().intValue()){ //房型数据 验证逻辑
                List<CesAllDayRuleMinute>  roomlayout = cesAllDayPriceRuleDto.getRoomLayoutRules();
                if(CollectionUtils.isEmpty(roomlayout)){
                    return Result.error(ResultCode.PARAM_MISS,"按照房型配置数据不能为空!");
                }
                //循环去校验规则
                for (CesAllDayRuleMinute v : roomlayout) {
                    //写校验规则
                    if (StringUtils.isBlank(v.getRoomLayoutId())){
                        return Result.error(ResultCode.PARAM_MISS);
                    }
                    if (null==v.getMinute()||1>v.getMinute().intValue()){
                        return Result.error(ResultCode.PARAM_MISS);
                    }
                    if (null==v.getPrice()||BigDecimal.ZERO.compareTo(v.getPrice())>0){
                        return Result.error(ResultCode.PARAM_MISS);
                    }
                    if (null==v.getMoreThenMinute()||1>v.getMoreThenMinute().intValue()){
                        return Result.error(ResultCode.PARAM_MISS);
                    }
                    if (null==v.getMoreThenPrice()||BigDecimal.ZERO.compareTo(v.getMoreThenPrice())>0){
                        return Result.error(ResultCode.PARAM_MISS);
                    }
                }
            }
        }

        if (StringUtils.isBlank(cesAllDayPriceRuleDto.getDayTime())||Integer.parseInt(cesAllDayPriceRuleDto.getDayTime())<0){
            return Result.error(ResultCode.PARAM_MISS);
        }

        try{
            return cesAllDayPriceRuleService.modifyWholeDayUnifyPrice(cesAllDayPriceRuleDto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value="获取", notes="获取")
    @PostMapping(value = "/fetch")
    public Result fetch(String hotelId){
        if(StringUtils.isBlank(hotelId)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return cesAllDayPriceRuleService.fetch(hotelId);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

}

